﻿using KidChoreApp.Data.IDataAdapters.Adapters;
using KidChoreApp.Data.IDataAdapters.Interfaces;
using KidChoreApp.DataModels;
using KidChoreApp.Web.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace KidChoreApp.Web.Controllers
{
    [Authorize]
    public class KidController : Controller
    {
        IKidChoreAdapter _adapter = new KidChoreAdapter();
        [AllowAnonymous]
        public ActionResult KidIndex()
        {
            List<Kid> kids = _adapter.GetAllKids();
            return View(kids);
        }
        [HttpGet]
        public ActionResult AddKid()
        {
            if (User.IsInRole("Admin"))
            {
            return View();
            }
            return RedirectToAction("KidIndex");
        }
        [HttpPost]
        public ActionResult AddKid(string name, int grade, string teacher)
        {
            if (User.IsInRole("Admin"))
            {
                Kid newKid = new Kid();
                newKid.Name = name;
                newKid.Grade = grade;
                newKid.Teacher = teacher;
                newKid = _adapter.AddKid(newKid);
                return RedirectToAction("KidDetail/" + newKid.Id);
            }
            return RedirectToAction("KidIndex");
        }
        public ActionResult KidDetail(int id)
        {
            KidDetailVM vm = new KidDetailVM();
            vm.Kid = _adapter.GetKid(id);
            vm.Chores = _adapter.GetChoresForKid(vm.Kid.Id);
            return View(vm);
        }

        [HttpPost]
        public ActionResult RemoveChoreFromKid(int kidId, int choreId)
        {
            if (User.IsInRole("Admin"))
            {
            _adapter.DeleteKidChore(kidId, choreId);
            return RedirectToAction("KidDetail/" + kidId);
            }
            return RedirectToAction("KidDetail/" + kidId);
        }
        [HttpGet]
        public ActionResult AddChoreToKid(int id)
        {
            if (User.IsInRole("Admin"))
            {
                AddChoreToKidVM vm = new AddChoreToKidVM();
                vm.Kid = _adapter.GetKid(id);
                vm.ChoresAssigned = _adapter.GetChoresForKid(vm.Kid.Id);
                vm.ChoresUnassigned = _adapter.GetAllChores();
                //Alex Method
                //foreach (var chore in vm.ChoresUnassigned)
                //{
                //    if (vm.ChoresAssigned.Contains(chore))
                //    {
                //        vm.ChoresUnassigned.Remove(chore);
                //    }
                //}

                for (int i = 0; i < vm.ChoresAssigned.Count(); i++)
                {
                    for (int j = 0; j < vm.ChoresUnassigned.Count(); j++)
                    {
                        if (vm.ChoresAssigned[i].Id == vm.ChoresUnassigned[j].Id)
                        {
                            vm.ChoresUnassigned.Remove(vm.ChoresUnassigned[j]);
                            break;
                        }
                    }
                }

                return View(vm);
            }
            return RedirectToAction("KidDetail/" + id);
        }
        [HttpPost]
        public ActionResult AddChoreToKid(int kidId, int choreId)
        {
            if (User.IsInRole("Admin"))
            {
                Kid kid = _adapter.GetKid(kidId);
                Chore chore = _adapter.GetChore(choreId);
                _adapter.AddKidChore(kid, chore);

                return RedirectToAction("AddChoreToKid/" + kidId);
            }
            return RedirectToAction("KidDetail/" + kidId);
        }
        public ActionResult DeleteKid(int id)
        {
            if (User.IsInRole("Admin"))
            {
                _adapter.DeleteKid(id);
                return RedirectToAction("KidIndex");
            }
            return RedirectToAction("KidDetail/" + id);
        }
        [HttpGet]
        public ActionResult EditKid(int id)
        {
            if (User.IsInRole("Admin"))
            {
                return View(_adapter.GetKid(id));
            }
            return RedirectToAction("KidDetail/" + id);
        }
        [HttpPost]
        public ActionResult EditKid(Kid kid)
        {
            if (User.IsInRole("Admin"))
            {
                kid = _adapter.UpdateKid(kid);
            }
            return RedirectToAction("KidDetail/" + kid.Id);
        }
    }
}